package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hee.erp.constant.ConstDictType;
import com.hee.erp.modules.entity.*;
import com.hee.erp.modules.mapper.SysSalaryMapper;
import com.hee.erp.modules.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.vo.page.SalaryPageParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-05-27
 */
@Service
public class SysSalaryServiceImpl extends ServiceImpl<SysSalaryMapper, SysSalary> implements ISysSalaryService {

    @Autowired
    ISysDictValueService sysDictValueService;

    @Autowired
    ISysCompanyService sysCompanyService;

    @Autowired
    ISysDepartService sysDepartService;

    @Autowired
    ISysUserService sysUserService;

    @Override
    public IPage listByPage(SalaryPageParams params) {
        IPage<SysSalary> page = new Page<>();
        page.setCurrent(params.getCurrent());
        page.setSize(params.getPageSize());

        QueryWrapper<SysSalary> queryWrapper = createQueryWrapper(params);
        this.page(page, queryWrapper);

        List<SysSalary> records = page.getRecords();
        setForeignKeys(records);
        return page;
    }

    @Override
    public SysSalary getDetailById(String id) {
        SysSalary salary = this.getById(id);
        setCommonForeignKey(salary);
        setDetailForeignKey(salary);
        return salary;
    }

    @Override
    public void saveDetail(SysSalary entity) {
        SysUser sysUser = sysUserService.getById(entity.getUserId());
        entity.setCompanyId(sysUser.getCompanyId());
        entity.setDepartId(sysUser.getDepartId());
        entity.setRoleName(sysUser.getRoleName());
        this.saveOrUpdate(entity);
    }

    private void setDetailForeignKey(SysSalary salary) {

    }

    private void setForeignKeys(List<SysSalary> records) {
        for(SysSalary salary : records) {
            setCommonForeignKey(salary);
        }
    }

    private void setCommonForeignKey(SysSalary salary) {
        //职工号、姓名
        if(StringUtils.isNotBlank(salary.getUserId())) {
            SysUser sysUser = sysUserService.getById(salary.getUserId());
            if(sysUser != null) {
                salary.setUserName(sysUser.getUserName());
                salary.setUserNo(sysUser.getUserNo());
            }
        }
        //工资类型
        if(salary.getSalaryType() != null) {
            SysDictValue dictValue = sysDictValueService.getByTypeAndValue(ConstDictType.SALARY_TYPE, salary.getSalaryType());
            if(dictValue != null) {
                salary.setSalaryTypeName(dictValue.getDictName());
            }
        }
        //公司
        if(StringUtils.isNotBlank(salary.getCompanyId())) {
            SysCompany company = sysCompanyService.getById(salary.getCompanyId());
            if(company != null) {
                salary.setCompanyName(company.getCompanyName());
            }
        }
        //部门
        if(StringUtils.isNotBlank(salary.getDepartId())) {
            SysDepart depart = sysDepartService.getById(salary.getDepartId());
            if(depart != null) {
                salary.setDepartName(depart.getDepartName());
            }
        }
        //发放人员payUserName
        if(StringUtils.isNotBlank(salary.getPayUserId())) {
            SysUser user = sysUserService.getById(salary.getPayUserId());
            if(user != null) {
                salary.setPayUserName(user.getUserName());
            }
        }
        //发放方式
        if(salary.getPayChannel() != null) {
            SysDictValue dictValue = sysDictValueService.getByTypeAndValue(ConstDictType.SALARY_PAY_CHANNEL, salary.getPayChannel());
            if(dictValue != null) {
                salary.setPayChannelName(dictValue.getDictName());
            }
        }
    }

    private QueryWrapper<SysSalary> createQueryWrapper(SalaryPageParams params) {
        QueryWrapper<SysSalary> queryWrapper = new QueryWrapper<>();
        if(params.getCondition() != null) {
            if(StringUtils.isNotBlank(params.getCondition().getBeginSalaryMonth())) {
                queryWrapper.lambda().ge(SysSalary::getSalaryMonth, params.getCondition().getBeginSalaryMonth());
            }
            if(StringUtils.isNotBlank(params.getCondition().getEndSalaryMonth())) {
                queryWrapper.lambda().le(SysSalary::getSalaryMonth, params.getCondition().getEndSalaryMonth());
            }
        }
        return queryWrapper;
    }
}
